package com.asset.salesforce.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.asset.salesforce.model.Account;
import com.asset.salesforce.model.Contact;
import com.asset.salesforce.model.ContactData;
import com.asset.salesforce.model.ContactList;
import com.asset.salesforce.model.Employee;
import com.asset.salesforce.model.Lead;
import com.asset.salesforce.model.LeadDetails;
import com.asset.salesforce.service.AccountService;
import com.asset.salesforce.service.AppointmentService;
import com.asset.salesforce.service.CalendarService;
import com.asset.salesforce.service.CallService;
import com.asset.salesforce.service.CityService;
import com.asset.salesforce.service.CompanyService;
import com.asset.salesforce.service.ContactService;
import com.asset.salesforce.service.CountryService;
import com.asset.salesforce.service.EmployeeService;
import com.asset.salesforce.service.LeadService;


@Controller
public class ContactController {
	@Autowired
	private EmployeeService employeeService;
	@Autowired
	private CountryService countryService;
	@Autowired
	private CityService cityService;
	@Autowired
	private CallService callService;
	@Autowired
	private CalendarService calendarService;
	@Autowired
	private AppointmentService appointmentService;
	@Autowired
	private LeadService leadService;
	@Autowired
	private ContactService contactService;
	@Autowired
	private AccountService accountService;
	@Autowired
	private CompanyService companyService;
	
//	@RequestMapping(value="/getAllContacts/{empId}", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
//	@ResponseBody
//	public List<ContactData> getAllLeads(@PathVariable String empId){
//		List<Contact> contacts = contactService.getAllContactByEmpId(Integer.parseInt(empId));
//		List<ContactData>contactData = new ArrayList<ContactData>();
//		for (Contact contact : contacts) {
//			ContactData data = new ContactData();
//			data.setfName(contact.getfName());
//			data.setParentName(contact.getParentName());
//			data.setMobile(contact.getMobile());
//			contactData.add(data);
//		}
//		return contactData;
//	}
	@RequestMapping(value="/getAllContacts/{empId}", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
	@ResponseBody
	public ContactList getAllLeads(@PathVariable String empId){
		List<Contact>contacts = contactService.getAllContactByEmpId(Integer.parseInt(empId));
		ContactList contactList = new ContactList();
		List<ContactData>contactData = new ArrayList<ContactData>();
		for (Contact contact : contacts) {
			ContactData data = new ContactData();
			data.setId(contact.getId());
			data.setfName(contact.getfName());
			String phoneMobile = null ;
			if(contact.getMobile()!=null&&!contact.getMobile().equalsIgnoreCase("")){
				phoneMobile=contact.getMobile();
			}
			if(contact.getPhone()!=null&&!contact.getPhone().equalsIgnoreCase("")){
				phoneMobile+=", "+contact.getPhone();
			}
			if(phoneMobile!=null){
				data.setMobile(phoneMobile);
			}
			data.setParentName(contact.getParentName());
			
			if(contact.getEmail()!=null&&!contact.getEmail().equalsIgnoreCase("")){
				data.setEmail(contact.getEmail());
			}
			Lead lead = leadService.getLeadById(contact.getParentId());
			Account account = accountService.getAccount(contact.getParentId());
			if(contact.getViewType().equals("lead")){
				data.setAccountName(lead.getfName());
			}
			else{
				data.setAccountName(account.getAccountName());
			}
			contactData.add(data);
		}
		contactList.setContactData(contactData);
		return contactList;
	}
	@RequestMapping(value="/getContact/{id}", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
	@ResponseBody
	public Contact getContact(@PathVariable String id){
		Contact contact = contactService.getContactById(Integer.parseInt(id));
		Employee employee = employeeService.getById(contact.getCreator().getId());
		if(employee!=null){
			contact.setCreatorName(employee.getfName()+" "+employee.getlName());
		}
		Lead lead = leadService.getLeadById(contact.getParentId());
		Account account = accountService.getAccount(contact.getParentId());
		if(contact.getViewType().equals("lead")){
			contact.setCompanyName(lead.getCompanyName());
			String accountName = lead.getfName();
			if(lead.getlName()!=null&&!lead.getlName().equalsIgnoreCase("")){
				accountName+=" "+lead.getlName();
			}
			contact.setAccountName(accountName);
		}
		else{
			contact.setCompanyName(account.getCompanyName());
			contact.setAccountName(account.getAccountName());
		}
		return contact;
	}
	
	@RequestMapping(value="/addContactGet/{empId}", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
	@ResponseBody
	public LeadDetails addContactGet(@PathVariable String empId){
		Employee employee = employeeService.getById(Integer.parseInt(empId));
		LeadDetails lead = new LeadDetails();
		if(employee!=null){
			lead.setOwnerName(employee.getfName()+" "+employee.getlName());
		}
		return lead;
	}
	
	@RequestMapping(value="/addContact",method=RequestMethod.POST,consumes="application/json;charset=UTF-8")
	@ResponseBody
	public Contact addContact(@RequestBody Contact contact){
		System.out.println("sucess");
		if(contact.getCreatorId()!=null){
			Employee contactOwner = employeeService.getById(contact.getCreatorId());
			contact.setCreator(contactOwner);
		}
		contactService.addContact(contact);
		return contact;
	}
}
